﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hostal.Bussines.Entities;
using Hostal.Bussines.Repository.Service;

namespace Hostal.Bussines.Repository
{
   public  class ProveedorRepository: MasterRepository,IProveedorRepository
    {

        public List<Proveedore> GetAllFromProveedor()
        {
            var query = from p in Context.Proveedor
                        select p;

            return query.ToList();
        }

        public List<Proveedore> GetAllFromProveedorbyRUC(string ruc)
        {
            var query = from p in Context.Proveedor
                        select p;

            if (ruc != null && ruc != "")
            {
                query = from p in query
                        where p.RUC.ToUpper().Contains(ruc.ToUpper())
                        select p;
            }

            return query.ToList();
        }

        public List<Proveedore> GetAllFromProveedorbyRazonSocial(string razon)
        {
            var query = from p in Context.Proveedor
                        select p;

            if (razon != null && razon != "")
            {
                query = from p in query
                        where p.RazonSocial.ToUpper().Contains(razon.ToUpper())
                        select p;
            }

            return query.ToList();
        }

        public Proveedore GetFromProveedorById(int id)
        {
            return Context.Proveedor.Find(id);
        }

        public void AddProveedor(Proveedore proveedor)
        {
            Context.Proveedor.Add(proveedor);
            Context.SaveChanges();
        }

        public void UpdateProveedor(Proveedore proveedor)
        {
            Context.Entry(proveedor).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveProveedor(int id)
        {
            var existe = Context.Proveedor.Where(p => p.IdProveedor.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                Context.Proveedor.Remove(existe);
                Context.SaveChanges();
            }
        }
    }
}
